package com.newegg.datafeed.adserving.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.newegg.datafeed.adserving.domain.User;

@Mapper
public interface UserMapper {
	
	@Select("SELECT username,nickname,company,password,company_name,email,phone,disable,memo,create_date,update_date "
			+ "FROM user where username=#{username} AND password=#{password} AND disable=false")
    @Results({
    	@Result(property = "companyName",  column = "company_name"),
    	@Result(property = "createDate",  column = "create_date"),
    	@Result(property = "updateDate",  column = "update_date")
    })
	User login(@Param("username") String username,@Param("password") String password);

	@Select("SELECT username,nickname,company,password,company_name,email,phone,disable,memo,create_date,update_date FROM user")
    @Results({
    	@Result(property = "companyName",  column = "company_name"),
    	@Result(property = "createDate",  column = "create_date"),
    	@Result(property = "updateDate",  column = "update_date")
    })
	List<User> getAll();
	
	@Insert("INSERT INTO user(username,nickname,company,password,company_name,email,phone,disable,memo,create_date,update_date) VALUES (#{username},#{nickname},#{company},#{password},#{companyName},#{email},#{phone},#{disable},#{memo},#{createDate},#{updateDate});")
    void insert(User user);
	
	@Update("UPDATE user SET nickname=#{nickname},company=#{company},password=#{password},company_name=#{companyName},email=#{email},phone=#{phone},disable=#{disable},memo=#{memo},create_date=#{createDate},update_date=#{updateDate} WHERE username=#{username}")
    void update(User user);
	
	@Delete("DELETE FROM user WHERE username=#{username}")
    void delete(String username);

	@Select("SELECT username,nickname,company,password,company_name,email,phone,disable,memo,create_date,update_date FROM user where username=#{username}")
    @Results({
    	@Result(property = "companyName",  column = "company_name"),
    	@Result(property = "createDate",  column = "create_date"),
    	@Result(property = "updateDate",  column = "update_date")
    })
	User get(@Param("username")String username);

	@Select("SELECT count(username) FROM user where company=#{company}")
	Long count(@Param("company") int company);

	@Select("SELECT username,nickname,company,password,company_name,email,phone,disable,memo,create_date,update_date FROM user where company=#{company} limit #{start},#{size}")
    @Results({
    	@Result(property = "companyName",  column = "company_name"),
    	@Result(property = "createDate",  column = "create_date"),
    	@Result(property = "updateDate",  column = "update_date")
    })
	List<User> page(@Param("company")Integer company,@Param("start") int start,@Param("size") Integer pageSize);

	@Delete("DELETE FROM user WHERE company=#{company}")
	void deleteByCompany(@Param("company")Integer company);
}
